<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<title>course_add</title>
		<link rel="shortcut icon" href="../../images/my/favicon.ico" />
		<link rel="bookmark" href="../../images/my/favicon.ico" />
		<link rel="stylesheet" href="../../statics/layui/css/layui.css" />
		<link rel="stylesheet" href="../../css/admin/common.css" />
		<link rel="stylesheet" href="../../css/admin/update_my_base_info.css" />
		
	</head>

	<body style="background: #F6F6F6;">
		<div class="layui-fluid">
			<div class="layui-row">
				<div class="layui-col-md12">
					<div class="layui-card layui-anim layui-anim-upbit">
						<div class="layui-card-header">教师选课
							<span class="btn_right">
								<!--<button type="button" id="back" title="返回" class="layui-btn layui-btn-normal layui-btn-xs" onclick="javascript:window.history.back(-1);"><i class="layui-icon">&#xe65c;</i>返回</button>-->
								<button type="button" id="refresh" title="刷新" class="layui-btn layui-btn-normal layui-btn-xs"><i class="layui-icon layui-icon-refresh"></i></button>
							</span>
						</div>
						<div class="layui-card-body">
							<form class="layui-form" action="">
								<div class="layui-form-item layui-row">
									<label class="my_layui-form-label layui-col-md1 layui-col-sm1 layui-col-xs12">选择教师：</label>
									<div class="layui-input-inline">
								        <select name="teacher" id="teacher" lay-verify="required" lay-search="" lay-filter="teacher_change">
								          
								        </select>
      							</div>
						</div>
								<div class="layui-form-item layui-row">
									<label class="my_layui-form-label layui-col-md1 layui-col-sm1 layui-col-xs12">选择课程：</label>
									<div class="layui-col-md10 layui-col-sm10 layui-col-xs12" id="course">

									</div>
								</div>
								
								<div class="layui-form-item layui-row">
									<label class="my_layui-form-label layui-col-md1 layui-col-sm1 layui-col-xs1 layui-hide-xs">&nbsp;</label>
									<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo"></i><span>保存修改</span></button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="../../statics/layui/layui.js"></script>
		<script type="text/javascript" src="../../statics/lay/system/admin/public.js" ></script>
		<script>
			layui.use(['layer', 'form'], function() {
				var layer = layui.layer,
					$ = layui.$,
					form = layui.form;
				
	
				initData();
				//表单监听提交
				form.on('submit(formDemo)', function(data) {
					
					
					var user_data = data.field;
					var tId = $("#teacher").val();
					var tName = $("#teacher").find("option:selected").text();
					var cIds = [];
					$.each($("#course").children('input:checked'), function(idx,obj) {
							cIds.push($(this).data('id'))
					});
					var user_data={
						tId:tId,
						cIds:cIds,
						tName:tName
					}
					user_data.cIds=cIds;
					user_data=JSON.stringify(user_data)
					
					$.ajax({
						url: IP + '/api/teacher_eav/sysTeacherCourse',
						type: 'POST',
						data: user_data,
						success: function(result, status, xhr) {
							layer.closeAll('loading');
							layer.msg(result.message, {
								icon: 1,
								time: 1000
							},function(){
								window.location.reload();
							});
						}
					});
					return false;
				});
				
					//初始化页面数据
				function initData() {
					showTeacher();
					
					
				}
				//select监听事件，老师选中后，通过改变事件。获取老师的tId，去查询老师选择了哪些课程
				
				form.on('select(teacher_change)', function(data){
					var tId=data.value;
					showCourseCheckbox("course",tId);
					/*form.render();*/
				    showCourse(tId);
					form.render();
				});
				//老师列表，模糊搜索选中老师
				function showTeacher(){
					$.ajax({
						url: IP+ '/api/teacher_eav/sysTeacher?pageSize=9999999',
						type: 'GET',
						async: false,
						success: function(result, status, xhr) {
							layer.closeAll('loading');
							var choice = "<option value=\"\">请选择教师</option>";
							var option="";
							for(var i=0;i<result.data.length;i++){
								option+="<option value='"+result.data[i].tId+"'>"+result.data[i].tName+"</option>";
								}
							$("#teacher").append(choice+option);
							form.render();
							
						}
					});
				}
				//从中间表查询老师选择了哪些课程，然后渲染。老师选中后，通过改变事件。获取得到老师的tId，去查询老师选择了哪些课程
				function showCourse(tId) {
					$.ajax({
						url: IP + '/api/teacher_eav/sysTeacherCourse/'+tId,
						type: 'GET',
						success: function(result, status, xhr) {
							layer.closeAll('loading');
						
							$.each(result.data, function(idx,obj) {
								$("input[type='checkbox'][data-id='"+obj.cId+"']").attr("checked","checked")
							});
							form.render();
						}
					});
				}
				
				
				});
				

			
		</script>
	</body>

</html>